package storage.impl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import spider.entitys.Novel;
import spider.interfaces.INovelSpider;
import spider.util.NovelSpiderFactory;
import storage.storage.Processor;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * created by yb
 * on 2017/8/16.
 **/
public abstract class AbstractNovelStorage implements Processor {
    protected SqlSessionFactory sqlSessionFactory;
    protected   Map< String, String > tasks = new TreeMap<>();

    public AbstractNovelStorage() throws FileNotFoundException {
        sqlSessionFactory = new SqlSessionFactoryBuilder().build( new FileInputStream( "src/conf/sqlMapConfig.xml" ) );
    }

    public void process(  ) {
        ExecutorService service = Executors.newFixedThreadPool( tasks.size() );
        List< Future< String > > futures = new ArrayList<>( tasks.size() );
        for ( Map.Entry< String, String > entry : tasks.entrySet() ) {
            final String key = entry.getKey();
            final String value = entry.getValue();
            futures.add( service.submit( new Callable< String >() {
                @Override
                public String call() throws Exception {
                    INovelSpider spider = NovelSpiderFactory.getNovelSpider( value );
                    Iterator< List< Novel > > iterator = spider.iterator( value, 10 );
                    while ( iterator.hasNext() ) {
                        System.err.println( "开始抓取   " + spider.next() );
                        List< Novel > novels = iterator.next();
                        for ( Novel novel : novels ) {
                            novel.setFirstLetter( key.charAt( 0 ) );
                        }
                        SqlSession session = sqlSessionFactory.openSession();
                        session.insert( "batchInsert", novels );
                        session.commit();
                        session.close();
                        Thread.sleep( 3_000 );
                    }
                    return key;
                }
            } ) );
        }
        service.shutdown();
        for ( Future< String > future : futures ) {
            try {
                System.out.println( "抓取" + future.get() + "成功" );
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }
    }
}
